<?php

namespace common\models\book;

use Yii;

/**
 * This is the model class for table "{{%stage_date}}".
 *
 * @property int $id
 * @property string $date
 * @property string $stages
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 */
class StageDate extends \common\db\ActiveRecord {

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%stage_date}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['date'], 'safe'],
            [['status', 'created_at', 'updated_at'], 'integer'],
            [['stages'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'date' => 'Date',
            'stages' => 'Stages',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public static function findDisableDate($date, $stages = null) {
        $query = static::findActive(static::STATUS_ACTIVE)->andWhere([
            'date' => $date,
        ]);
        if ($stages) {
            $expression = new \yii\db\Expression('FIND_IN_SET(' . $stages . ', `stages`)');
            $query->andWhere([
                'or',
                ['stages' => null],
                $expression,
            ]);
        } else {
            $query->andWhere(['stages' => $stages]);
        }
        $query->debug();
        return $query->exists();
    }

}
